{% extends 'accounts/asset_add.html' %}

{% block user_assets %}
  {% include 'accounts/user_assets_bot.html' %}
{% endblock %}

{% block account-intro %}
  <p class="narrow">
  Add an IRC bot to your account. If no bot exists for the IRC channel
  you specify, one will be created automatically.
  </p>

  <p class="narrow">
  Bots will report commit messages in real-time.  After adding a bot
  you can define which commits it reports and how those messages
  are formatted.
  </p>
{% endblock %}

{% block account-forms %}
<form action="{{ request.path }}" method="post">
  <fieldset><legend>IRC Bot</legend>

  <div class="form-row">
    <label for="id_network">Network:</label>
    <select name="network" id="id_network" size="8">
      {% for network in networks %}
        <option value="{{ network.id }}"
          {% ifequal network.id_string form.data.network %}
          selected="1"{% endifequal %}>
          {{ network|escape }}
        </option>
      {% endfor %}
      <option value="_other"
          {% ifequal form.data.network "_other" %}
          selected="1"{% endifequal %}>
          Other...
          </option>
    </select>
    {% if form.errors.network %}
      <div class="error">{{ form.errors.network.0 }}</div>
    {% else %}
      <div class="select-note" id="id_network_details">Foo</div>
    {% endif %}
  </div>

  <div id="new_network_form"
    {% ifnotequal form.data.network "_other" %}
    style="display: none;"{% endifnotequal %}>

    <div class="form-row">
      <label for="id_netname">New network name:</label>
      <input type="text" class="text" name="netname" id="id_netname" value="{{ form.data.netname|escape }}"/>
      <div class="error">{{ form.errors.netname.0 }}</div>
    </div>

    <div class="form-row">
      <label for="id_server">Server:</label>
      <input type="text" class="text" name="server" id="id_server" value="{{ form.data.server|escape }}"/>
      <div class="error">{{ form.errors.server.0 }}</div>
    </div>
  </div>

  <div class="form-row">
    <label for="id_channel">Channel:</label>
    <input type="text" class="text" name="channel" id="id_channel" value="{{ form.data.channel|escape }}"/>
    <div class="error">{{ form.errors.channel.0 }}</div>
  </div>

  <div class="submit-row">
    <div>&raquo;</div> <input type="submit" value="Add" />
  </div>

  </fieldset>
</form>
{% endblock %}

{% block scripts-init %}
  {{ block.super }}

  var network_details = new Object();
  {% for network in networks %}
  network_details["{{ network.id }}"] = "{{ network|escape|addslashes }}<br />{{ network.uri|escape|addslashes }}";
  {% endfor %}

  var network = document.getElementById('id_network');
  network.focus();
  network.onchange = function () {
    var form = document.getElementById('new_network_form');
    var selected = network.options[network.selectedIndex].value;

    form.style.display = (selected == '_other') ? 'block' : 'none';

    var detailElement = document.getElementById('id_network_details');
    if (detailElement) {
      detailElement.innerHTML = network_details[selected] || "";
    }   
  }
  if (network.selectedIndex < 0) {
    network.selectedIndex = 0;
  }
  network.onchange();
{% endblock %}
